package com.cjm.algorithm.node;

import com.cjm.algorithm.ListNode;
import com.cjm.algorithm.ListNodeHelp;

public class Ti24050619 {
    public static void main(String[] args) {
        ListNode listNode = ListNodeHelp.toListNode(new int[]{1,2,3,4,5});
        Ti24050619 t = new Ti24050619();
        ListNode listNode1 = t.removeNthFromEnd(listNode, 2);
        System.err.println("OK");
    }

    public ListNode removeNthFromEnd(ListNode head, int n) {
        // 这里是需删除倒数第n个节点, 所以我们顺序的处理好
        ListNode dummy = new ListNode(0);
        dummy.next = head;

        ListNode fast = dummy;
        ListNode slow = dummy;

        fast = fast.next;
        while (n > 0 && fast != null) {
            fast = fast.next;
            n--;
        }

        while (fast != null) {
            fast = fast.next;
            slow = slow.next;
        }

        slow.next = slow.next.next;
        return dummy.next;
    }
}
